//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner) ,m_HexConvert(false), m_DecConvert(false)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Memo1Change(TObject *Sender)
{
    if (m_HexConvert) return;
    m_DecConvert = true;
    Memo2->Lines->Clear();
    AnsiString s;
    char temp[128];
    //
    for(int i = 0; i < Memo1->Lines->Count; i++)
    {
        uint64 v;
        if (TryStringToDDWord(Memo1->Lines->Strings[i].c_str(), v))
        {
            Memo2->Lines->Add(DDWordToHex(v, temp, 1, 'A', false));
        }
        else
        {
            Memo2->Lines->Add("??");
        }
    }
    m_DecConvert = false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Memo2Change(TObject *Sender)
{
    if (m_DecConvert) return;
    m_HexConvert = true;
    Memo1->Lines->Clear();
    AnsiString s;
    char temp[128];
    //
    for(int i = 0; i < Memo2->Lines->Count; i++)
    {
        uint64 v;
        s = "0x" + Memo2->Lines->Strings[i];
        if (TryStringToDDWord(s.c_str(), v))
        {
            Memo1->Lines->Add(DDWordToStr(v,temp));
        }
        else
        {
            Memo1->Lines->Add("??");
        }
    }
    m_HexConvert = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1Change(TObject *Sender)
{
    //
    AnsiString strResult = "??";
    do
    {
        AnsiString strTmp = Edit1->Text.Trim().c_str();
        uint64 dest;
        if (!TryStringToDDWord(Edit1->Text.Trim().c_str(), dest))
        {
            break;
        }

        if (dest < 0xffffffff )
        {
            dest *= 1000;
        }
        if (dest < MIN_MILLIS || dest >= MAX_MILLIS)
        {
            break;
        }
        int iYear, iMonth, iDay;
        int iHour, iMillis, iMinute, iSecond;
        dest += DAYS_1970_1_1 * MILLIS_PRE_DAY64;
        dest += ComboBox1->Text.ToInt() * 3600 * 1000; 

        DateTimeMillisToDateTime((int64)dest, iYear, iMonth, iDay, iHour, iMinute, iSecond, iMillis);
        strResult.printf("%04d-%02d-%02d %02d:%02d:%02d.%03d", iYear, iMonth, iDay, iHour, iMinute, iSecond, iMillis);
        
        //MillisToDateTime();
    }while(False);

    Edit2->Text = strResult;
}
//---------------------------------------------------------------------------



